﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using DanTriNews.Resources;
using DanTriNews.ViewModels;
using DanTriNews.Src;
using DanTriNews.Src.Helper;
using System.Windows.Media;

namespace DanTriNews
{
    public partial class MainPage : PhoneApplicationPage
    {
        public static MainPage instance;
        public int currentcategory = 0;
        // Constructor
        public MainPage()
        {
            instance = this;
            InitializeComponent();
            DataContext = App.ViewModel;

        }

        // Load data for the ViewModel Items
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            prbLoad.Visibility = Visibility.Collapsed;
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData(currentcategory);
            }
            if (NavigationContext.QueryString.ContainsKey("CategoryId"))
            {
                int c = int.Parse(NavigationContext.QueryString["CategoryId"]);
                pvDanTri.SelectedItem = App.ViewModel.FindCategory(c);
            }
        }

        private void pvDanTri_LoadedPivotItem(object sender, PivotItemEventArgs e)
        {
            CategoryViewModel c = pvDanTri.SelectedItem as CategoryViewModel;
            currentcategory = c.Id;
            c.Loaddata();
        }

        private void Grid_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            string id = ((sender as Grid).FindName("tblId") as TextBlock).Text;
            NavigationService.Navigate(new Uri("/Src/ReadNews.xaml?NewsId=" + id + "&CategoryId=" + currentcategory, UriKind.Relative));
        }

        private void btnNext_Click(object sender, System.EventArgs e)
        {
            CategoryViewModel c = pvDanTri.SelectedItem as CategoryViewModel;
            c.Next();
            ScrollToTop();
        }
        private void ScrollToTop()
        {
            PivotItem pvitem = pvDanTri.ItemContainerGenerator.ContainerFromItem(pvDanTri.SelectedItem) as PivotItem;
            ScrollViewer sc = FindScrollViewer(pvitem);
            sc.ScrollToVerticalOffset(0);
        }
        private ScrollViewer FindScrollViewer(DependencyObject parent)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                var _Child = VisualTreeHelper.GetChild(parent, i);
                if (_Child is ScrollViewer)
                {
                    return _Child as ScrollViewer;
                }
                return FindScrollViewer(_Child);
            }
            return null;
        } 
        private void btnPrev_Click(object sender, System.EventArgs e)
        {
            CategoryViewModel c = pvDanTri.SelectedItem as CategoryViewModel;
            c.Prev();
            ScrollToTop();
        }

        private void btnRefresh_Click(object sender, System.EventArgs e)
        {
            CategoryViewModel ct = pvDanTri.SelectedItem as CategoryViewModel;
            ct.ReLoadData();
            ScrollToTop();
        }

        private void btnCategory_Click(object sender, System.EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Src/Selectcategory.xaml?CategoryId=" + currentcategory, UriKind.Relative));
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn thoát ứng dụng ?", "Thoát !", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                NavigationService.Navigate(new Uri("/Src/AdPage.xaml?Exit=1", UriKind.Relative));
            }
            else
            {
                e.Cancel = true;
            }
        }
        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            PhoneApplicationPage phoneApplicationPage = (PhoneApplicationPage)(((PhoneApplicationFrame)Application.Current.RootVisual)).Content;
            TransitionHelper.excuteTransition(phoneApplicationPage);
            base.OnNavigatingFrom(e);
        }
    }
}